package com.yy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yy.domain.Prod;
import com.yy.dto.PageDTO;
import com.yy.dto.ProdAddDTO;
import com.yy.dto.ProdQueryDTO;
import com.yy.model.Result;
import com.yy.service.ProdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "商品管理相关接口")
@RequestMapping("prod/prod")
public class ProdController {

    @Autowired
    private ProdService prodService;
    /**
     * 分页查询商品
     */
    @GetMapping("page")
    @ApiOperation("分页查询商品")
    @PreAuthorize("hasAuthority('prod:prod:page')")
    public Result<Page<Prod>> loadProdPage(PageDTO pageDTO, ProdQueryDTO prodQueryDTO){
        Page<Prod> prodPage=this.prodService.loadProdPage(pageDTO,prodQueryDTO);
        return Result.success(prodPage);
    }

    /**
     * 新增商品
     */
    @PostMapping
    @ApiOperation("新增商品")
    @PreAuthorize("hasAuthority('prod:prod:save')")
    public Result<String> addProd(@RequestBody @Validated ProdAddDTO prodAddDTO){
        Integer row=this.prodService.addProd(prodAddDTO);
        return Result.handle(row>0);
    }

    /**
     * 根据商品id查询商品信息
     */
    @GetMapping("getProdById")
    @ApiOperation("根据商品id查询商品信息")
    public Result<Prod> getProdById(@RequestParam("prodId") Long prodId){
        Prod prod=this.prodService.getById(prodId);
        return Result.success(prod);
    }
}
